package com.order.service.impl;

import com.order.dao.User;
import com.order.exception.ServiceException;
import com.order.mapper.UserMapper;
import com.order.service.UserService;
import com.order.utils.TokenUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Slf4j
@AllArgsConstructor
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

/*    public User selectByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);  //  eq => ==   where username = #{username}
        // 根据用户名查询数据库的用户信息
        return getOne(queryWrapper); //  select * from user where username = #{username}
    }*/

    //验证用户账户
    @Override
    public User user_login(User user){
        User dbUser = userMapper.getByUsername(user.getUsername());
        if(dbUser == null){
            //抛出异常
            throw new ServiceException("用户名或密码错误");
        }
        if(!user.getPassword().equals(dbUser.getPassword())){
            throw new ServiceException("用户名或密码错误");
        }
        // 生成token
        String token = TokenUtils.createToken(dbUser.getId().toString(),dbUser.getPassword());
        dbUser.setToken(token);
        return dbUser;
    }


}
